//JavaScript代码区域
layui.use(['layer', 'form', 'laydate', 'table'], function() {
	var layer = layui.layer,
		$ = layui.$,
		form = layui.form,
		laydate = layui.laydate;

	var matchid = getUrlParam("matchid")
	initData(matchid);
	$(function() {
		//添加按钮事件
		$('#adduser').on('click',function(){
			window.location='match_pk_add.html?matchid='+matchid
		})
		//场次状态开启关闭
		$('#matchpklist .one_match').on('click','.layui-form-switch',function(){
			var that=this;
			//对战状态；1:开启，0:关闭
			var pkid=$(that).parent().parent().siblings('input').val()
			var status=$(that).siblings('input').is(':checked');
			var checkstatus;
			var ts;
			if(status){
				checkstatus=1;
				ts="<a style='color:red'> 开启投票 </a>"
			}else{
				checkstatus=0
				ts="<a style='color:red'> 关闭投票 </a>"
			}
			layer.confirm("您真的要"+ts+"吗？",{icon:3}, function(index) {
				layer.close(index);
				$.ajax({
					url: IP + '/api/admin/matchpkinfo',
					type: "PUT",
					async:false,
					data: JSON.stringify({
						pkid:pkid,
						status:checkstatus
					}),
					success: function(result, status, xhr) {
						layer.closeAll('loading');
						layer.msg(result.msg, {
							icon: 1,
							time: 1000
						});
					}
				});
			},function(){
				if(status){
					$(that).siblings('input').prop('checked',false)
				}else{
					$(that).siblings('input').prop('checked',true)
				}
				form.render();
			});
		})
		//删除场次
		$('#matchpklist .one_match .deletematch').on('click','button',function(){
			var pkid=$(this).data('pkid');
			var uids=[];
	      	uids.push(pkid)
			layer.confirm("您真的要<a style='color:red'> 删除 <a>吗",{icon:3}, function(index) {
				layer.close(index);
				$.ajax({
					url: IP + '/api/admin/matchpkinfo',
					type: "DELETE",
					data: JSON.stringify(uids),
					success: function(result, status, xhr) {
						layer.closeAll('loading');
						layer.msg(result.msg, {
							icon: 1,
							time: 1000
						},function(){
							window.location.reload()
						});
					}
				});
			});
		})
	})
	function initData(matchid){
		$.ajax({
			url: IP + '/api/admin/matchpkinfo/'+matchid,
			type: "GET",
			async:false,
			success: function(result, status, xhr) {
				layer.closeAll('loading');
				$('#matchdate').html(result.data.match.matchdate+" 的 "+result.data.match.matchname)
				if(result.data.data.length==0){
					$('#matchpklist').append("<p class='error'>暂无比赛场次</p>")
					return false;
				}
				$.each(result.data.data, function(idx,obj) {
					var status="";
					if(obj.status==1){
						status="checked"
					}
					var html='<div class="one_match">'+
							'<input type="hidden" name="pkid" class="pkid" value="'+obj.pkid+'" />'+
							'<div class="layui-col-xs12 layui-col-sm12 layui-col-md12 head">'+
								'<span class="btn_right">'+
								'投票状态：'+
								'<input class="status" type="checkbox" name="isvisible" lay-skin="switch" lay-text="投票中|已关闭" '+status+'>'+
							'</span>'+
							'</div>'+
							'<div class="layui-col-xs12 layui-col-sm5 layui-col-md5">'+
								'<div class="layui-row">'+
									'<div class="layui-col-xs6 layui-col-sm6 layui-col-md6">'+
										'<div class="headimg">'+
											'<img src="'+obj.firstPlayer.headimgurl+'" />'+
										'</div>'+
									'</div>'+
									'<div class="layui-col-xs6 layui-col-sm6 layui-col-md6">'+
										'<div class="playuserinfo">'+
											'<span class="layui-badge layui-bg-orange">'+obj.firstPlayer.pno+'</span>'+
											'<p>姓名：<span>'+obj.firstPlayer.name+'</span></p>'+
											'<p>歌曲：<span>'+obj.firstsongname+'</span></p>'+
											'<p>观众投票数：<span>'+obj.firstticketcount+'</span></p>'+
											'<p>评委分数：<span>'+obj.firstneedscore+'</span></p>'+
											'<p>最终得分：<span>'+obj.firstscore+'</span></p>'+
										'</div>'+
									'</div>'+
								'</div>'+
							'</div>'+
							'<div class="layui-col-xs1 layui-col-sm1 layui-col-md1">'+
								'<p class="PK_LOGO">PK</p>'+
							'</div>'+
							'<div class="layui-col-xs12 layui-col-sm5 layui-col-md5">'+
								'<div class="layui-row">'+
									'<div class="layui-col-xs6 layui-col-sm6 layui-col-md6">'+
										'<div class="headimg">'+
											'<img src="'+obj.secondPlayer.headimgurl+'" />'+
										'</div>'+
									'</div>'+
									'<div class="layui-col-xs6 layui-col-sm6 layui-col-md6">'+
										'<div class="playuserinfo">'+
											'<span class="layui-badge layui-bg-orange">'+obj.secondPlayer.pno+'</span>'+
											'<p>姓名：<span>'+obj.secondPlayer.name+'</span></p>'+
											'<p>歌曲：<span>'+obj.secondsongname+'</span></p>'+
											'<p>观众投票数：<span>'+obj.secondticketcount+'</span></p>'+
											'<p>评委分数：<span>'+obj.secondneedscore+'</span></p>'+
											'<p>最终得分：<span>'+obj.secondscore+'</span></p>'+
										'</div>'+
									'</div>'+
								'</div>'+
							'</div>'+
							'<div class="layui-col-xs1 layui-col-sm1 layui-col-md1">'+
								'<p class="deletematch">'+
									'<button type="button" class="layui-btn layui-btn-sm layui-btn-danger" data-pkid="'+obj.pkid+'">删除此场次</button>'+
								'</p>'+
							'</div>'+
							'<div class="layui-clear"></div>'+
						'</div>';
					$('#matchpklist').append(html)
					form.render();
				});
			}
		});
	}
});